import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../CIFAR10_dataset", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
dataloader = DataLoader(dataset, batch_size=1)


class Wangqi(nn.Module):
    def __init__(self):
        super(Wangqi, self).__init__()
        self.modle1 = nn.Sequential(
            nn.Conv2d(3, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.modle1(x)
        return x


wangqi = Wangqi()
optim=torch.optim.SGD(wangqi.parameters(),lr=0.01)
loss = nn.CrossEntropyLoss()
for epoch in range(2):
    running_loss=0
    for data in dataloader:
        imgs, targets = data
        output = wangqi(imgs)
        result_loss = loss(output, targets)
        optim.zero_grad()
        result_loss.backward()
        optim.step()
        running_loss=result_loss+running_loss
    print(running_loss)
